<template>
    <div>
        <el-form :model="formDatas" ref="formDatas" class="el-form el-forms hcenter">
          <el-form-item prop="name">
            <el-input placeholder="供应商名称" v-model.trim="formDatas.name"></el-input>
          </el-form-item>
          <el-form-item prop="linkman">
            <el-input placeholder="联系人" v-model.trim="formDatas.linkman"></el-input>
          </el-form-item>
          <el-form-item prop="mobile">
            <el-input placeholder="联系电话" v-model.trim="formDatas.mobile"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearchSupplier">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAddSupplier">新增</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearchReset">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 表格 -->
        <el-table :data="tableData" height="250" border style="width: 100%">
          <el-table-column type="index" prop="index" label="序号" width="60"></el-table-column>
          <el-table-column prop="name" label="供应商名称"></el-table-column>
          <el-table-column prop="linkman" label="联系人" width="100"> </el-table-column>
          <el-table-column prop="mobile" label="联系电话"> </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleOpenEditDialog(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleRemoveSupplier(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="obj.pageNum"
          :page-sizes="[10, 20, 50]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
        <!-- 新增弹出框 -->
    <el-dialog title="供应商新增" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="addform" :rules="rules" ref="addform">
        <el-form-item label="供应商名称" prop="name" label-width="100px">
          <el-input v-model.trim="addform.name"></el-input>
        </el-form-item>
        <el-form-item prop="linkman" label="联系人" label-width="100px">
          <el-input v-model.trim="addform.linkman"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="联系电话" label-width="100px">
          <el-input v-model.trim="addform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="100px">
          <el-input type="textarea" v-model="addform.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetform('addform')">取 消</el-button>
        <el-button type="primary" @click="handleAddSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="供应商编辑" :visible.sync="dialogEditFormVisible" width="500px">
      <el-form :model="editform" :rules="rules" ref="editform">
        <el-form-item label="供应商名称" prop="name" label-width="100px">
          <el-input v-model.trim="editform.name"></el-input>
        </el-form-item>
        <el-form-item prop="linkman" label="联系人" label-width="100px">
          <el-input v-model.trim="editform.linkman"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="联系电话" label-width="100px">
          <el-input v-model.trim="editform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="100px">
          <el-input type="textarea" v-model="editform.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetform('editform')">取 消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确 定</el-button>
      </div>
    </el-dialog>
    </div>
</template>

<script>
import suppliserModel from '../api/supplier.js'
import myMain from '../components/myMain.vue'
export default {
  data() {
    return {
      rules: {
          name: [
            { required: true, message: '请输入供应商名称', trigger: 'blur' },
            {min :2, max : 10, message: "长度在 5 到 20 个字符",trigger: "blur"}
          ],
          linkman: [
            { required: true, message: '请输入联系人', trigger: 'blur' },
            {min :2, max : 10, message: "长度在 2 到 10 个字符",trigger: "blur"}
          ]
      },
      // 查询表单数据
      formDatas:{
        linkman : '',
        mobile: '',
        name:'',
      },
      // 表格数据
      tableData:[],
      // 新增弹框
      dialogFormVisible:false,
      // 新增表单内容
      addform:{
        name :'',
        linkman: '',
        mobile:'',
        remark: ''
      },
      obj: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      // 编辑弹框
      dialogEditFormVisible:false,
      // 编辑表单内容
      editform:{
        linkman:'',
        mobile: '',
        name:'',
        remark:''
      }
    }
  },
  created() {
    this.initSupplier()
  },
  methods: {
    async initSupplier(){
      const response = await suppliserModel.getSupplierList(this.obj,this.formDatas)
      // 总条数
      this.total = response.msg.count
      // 表格数据
      this.tableData = response.msg.rows
    },
    // 点击页码
    handleCurrentChange(page){
      this.obj.pageNum = page
      this.initSupplier()
    },
    // 点击条数
    handleSizeChange(size){
      this.obj.pageSize = size
      this.initSupplier()
    },
    // 点击重置
    handleSearchReset(){
      this.$refs["formDatas"].resetFields();
    },
    // 查询
    handleSearchSupplier() {
      this.initSupplier()
    },
    // 删除
    handleRemoveSupplier(row) {
      const supplierId = row.id
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const response = await suppliserModel.removeSupplier(supplierId)
        if(response.error_code == 0) {
          this.initSupplier()
          this.$message({
          type: 'success',
          message: '删除成功!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    //点击新增打开新增弹出框
    handleAddSupplier() {
      this.dialogFormVisible = true
    },
    // 点击确定实现新增
    handleAddSubmit() {
      this.$refs['addform'].validate(async valid => {
        if(!valid) return
        const response = await suppliserModel.addSupplier(this.addform)
        if(response.error_code == 0) {
          this.$message('新增成功')
          this.dialogFormVisible = false
          this.$refs["addform"].resetFields()
          this.initSupplier()
        }
      })
    },
    // 点击编辑打开编辑弹窗
    async handleOpenEditDialog(row) {
      const supplierId = row.id
      const response = await suppliserModel.findSupplier(supplierId)
      if(response.error_code == 0) {
        this.editform = response.msg
      }
      this.dialogEditFormVisible = true
    },
     // 表单重置方法
    resetform(formName) {
      this.$refs[formName].resetFields()
      if(formName === "addform") {
        this.dialogFormVisible = false
      }else{
        this.dialogEditFormVisible = false
      }
    },
    // 点击确定实现编辑
    handleEditSubmit(row){
      const supplierId = this.editform.id
      this.$refs['editform'].validate(async valid => {
        if(!valid) return
        const response = await suppliserModel.editSupplier(supplierId,this.editform)
        if(response.error_code == 0) {
          this.$message('更新成功')
          this.dialogEditFormVisible = false
          this.$refs["editform"].resetFields()
          this.initSupplier()
        }
      })
    },
  },
  components: {
    myMain
  }
}
</script>

<style scoped lang="scss">
.el-forms {
  width: 100%;
  height: 85px;
  align-items: flex-end !important;
  .el-form-item {
    margin-right: 10px !important;
  }
}
.el-select {
  width: 120px !important;
}
.el-table {
  height: 380px !important;
}
</style>